<?php

class LangUrlRule extends CUrlManager
{

	public function createUrl($route,$params=array(),$ampersand='&')
    {
//        if (!isset($params['language'])) {
//            if (Yii::app()->user->hasState('language'))
//                Yii::app()->language = Yii::app()->user->getState('language');
//            else if(isset(Yii::app()->request->cookies['language']))
//                Yii::app()->language = Yii::app()->request->cookies['language']->value;
//            $params['language']=Yii::app()->language;
//        }
        return parent::createUrl($route, $params, $ampersand);
    }
	
	protected function createUrlDefault($route,$params,$ampersand)
	{
		if($this->getUrlFormat()===self::PATH_FORMAT)
		{
			$url=rtrim($this->getBaseUrl().'/ru/'.$route,'/');
			if($this->appendParams)
			{
				$url=rtrim($url.'/'.$this->createPathInfo($params,'/','/'),'/');
				return $route==='' ? $url : $url.$this->urlSuffix;
			}
			else
			{
				if($route!=='')
					$url.=$this->urlSuffix;
				$query=$this->createPathInfo($params,'=',$ampersand);
				return $query==='' ? $url : $url.'?'.$query;
			}
		}
		else
		{
			$url=$this->getBaseUrl().'/ru';
			if(!$this->showScriptName)
				$url.='/';
			if($route!=='')
			{
				$url.='?'.$this->routeVar.'='.$route;
				if(($query=$this->createPathInfo($params,'=',$ampersand))!=='')
					$url.=$ampersand.$query;
			}
			else if(($query=$this->createPathInfo($params,'=',$ampersand))!=='')
				$url.='?'.$query;
			return $url;
		}
	}
	
	protected function createUrlRule($route,$pattern)
	{
		if(is_array($route) && isset($route['class']))
			return $route;
		else
			return new $this->urlRuleClass($route, '<lang:\w{2}>/' . $pattern);
	}
	
	public function parseUrl($request)
	{
		$route = parent::parseUrl($request);
		
		if(mb_eregi('^\w{2}/', $route, $matches))
		{
			if(!empty($matches[0]))
			{
				$matches[0] = trim($matches[0], '/');
				if(!isset(Yii::app()->params->locales[$matches[0]]))
					throw new CHttpException('Language {'. $matches[0] .'} don`t support');
				
				Yii::app()->language = $matches[0];
				
				$route = str_replace($matches[0] . '/', '', $route);
			}
		}
		
		$route = str_replace('ru/', '', $route);
		
		return $route;
//		return parent::parseUrl($request);
		
		if($this->getUrlFormat()===self::PATH_FORMAT)
		{
			$rawPathInfo=$request->getPathInfo();
			$pathInfo=$this->removeUrlSuffix($rawPathInfo,$this->urlSuffix);
			foreach($this->_rules as $i=>$rule)
			{
				if(is_array($rule))
					$this->_rules[$i]=$rule=Yii::createComponent($rule);
				if(($r=$rule->parseUrl($this,$request,$pathInfo,$rawPathInfo))!==false)
					return isset($_GET[$this->routeVar]) ? $_GET[$this->routeVar] : $r;
			}
			if($this->useStrictParsing)
				throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
					array('{route}'=>$pathInfo)));
			else
				return $pathInfo;
		}
		else if(isset($_GET[$this->routeVar]))
			return $_GET[$this->routeVar];
		else if(isset($_POST[$this->routeVar]))
			return $_POST[$this->routeVar];
		else
			return '';
	}
	
}